package spring.mymessage.controller;

import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import spring.mymessage.service.MyWebSocketServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import spring.mymessage.service.RedisBlockingQueue;

import java.io.IOException;

@RestController
@RequestMapping("/api/my-ws")
public class MyWebSocketController {
    @Autowired
    MyWebSocketServer webSocketServer;
    @Autowired
    RedisBlockingQueue blockingQueue;

    /**
     * 群发消息内容
     * @param message
     * @return
     */
    @RequestMapping(value="/sendAllMessageByDeptId", method= RequestMethod.GET)
    public String sendAllMessageByDeptId(@RequestParam(required=true) String message,String deptId){
        try {
            webSocketServer.saveMessageByDeptId(message,deptId);
            blockingQueue.sendMessageByDept(deptId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ok";
    }


    /**
     * 群发消息内容
     * @param message
     * @return
     */
    @RequestMapping(value="/sendAll", method= RequestMethod.GET)
    public String sendAllMessage(@RequestParam(required=true) String message){
        try {
            MyWebSocketServer.BroadCastInfo(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }
    
    /**
     * 指定会话ID发消息
     * @param message 消息内容
     * @param id 连接会话ID
     * @return
     */
    @RequestMapping(value="/sendOne", method= RequestMethod.GET)
    public String sendOneMessage(@RequestParam(required=true) String message, @RequestParam(required=true) String id){
        try {
            MyWebSocketServer.SendMessage(message,id);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }
}